from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import Qt, QString
from .msg_lamp import MiniLed

class BrassPlate(QtGui.QWidget):
        def __init__(self, *args, **kw):
		QtGui.QWidget.__init__(self, *args, **kw)

		self.inverted = False

	def mk_grad(self):
		grad = QtGui.QLinearGradient(0, 39, 25, 0)
		grad.setColorAt(0, QtGui.QColor(0x94, 0x41, 0x0f))
		grad.setColorAt(1, QtGui.QColor(0xcd, 0x52, 0x00))
		grad.setSpread(QtGui.QGradient.ReflectSpread)
		return grad

	def mk_pen(self):
		return QtGui.QPen( Qt.black, 1, 
			Qt.SolidLine, Qt.FlatCap, Qt.MiterJoin
		);

	def mk_qp(self):
		qp = QtGui.QPainter (self);
		qp.setRenderHint(QtGui.QPainter.Antialiasing, True)
		qp.setRenderHint(QtGui.QPainter.TextAntialiasing, True)
		qp.setRenderHint(
			QtGui.QPainter.HighQualityAntialiasing, True
		)
		return qp

	def paintEvent(self, e):
		grad = self.mk_grad()
		pen = self.mk_pen()
		qp = self.mk_qp()
		qp.setPen(pen);

		r = QtCore.QRect(self.rect())
		r.setWidth(r.width() - 1)
		r.setHeight(r.height() - 1)
		rb = QtCore.QRect(r)
		rb.setX(r.x()+1)
		rb.setY(r.y()+1)

		qp.setBrush(Qt.black)
		if self.inverted:
			qp.drawRect(r);
			qp.setBrush(grad);
			qp.drawRect(rb);
		else:
			qp.drawRect(rb);
			qp.setBrush(grad);
			qp.drawRect(r);

class BrassButton(QtGui.QWidget):
        def __init__(self, *args, **kw):
		QtGui.QWidget.__init__(self, *args, **kw)

		outer = BrassPlate(self)
		outer.setGeometry(0,0,114,32)

		inner = BrassPlate(self)
		inner.setGeometry(2,3,109,26)

		caption = QtGui.QLabel(self)
		caption.setGeometry(4,3,100,26)
		caption.setText("Boo!")
		caption.setAlignment(Qt.AlignVCenter|Qt.AlignHCenter)
		font = QtGui.QFont("Times New Roman", 16)
		caption.setFont(font)
		self.caption = caption

	def mousePressEvent(self, e):
		self.caption.move(3,4)
		self.inverted = True
		self.update()

	def mouseReleaseEvent(self, e):
		self.caption.move(2,3)
		self.inverted = False
		self.update()

if __name__ == "__main__":
	import sys
	app = QtGui.QApplication(sys.argv)

#
#	use the wallpaper as the top level widget
#
	top = QtGui.QLabel()
	top.setGeometry(0, 0, 300, 600)
	path = "resources/wood.jpg"
	top.setPixmap(QtGui.QPixmap(path))

	panel = BrassButton(top)
	panel.move(20, 20)

	top.show()
	sys.exit(app.exec_())
